<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://chenjiabing666.github.io/"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>


  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-flash.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />



  <meta name="google-site-verification" content="true" />








  <meta name="baidu-site-verification" content="code-ltfpVmGDr3" />







  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="杂谈," />





  <link rel="alternate" href="/atom.xml" title="不才陈某技术博客" type="application/atom+xml" />






<meta name="description" content="导读
Zookeeper 相信大家都听说过，最典型的使用就是作为服务注册中心。今天陈某带大家从零基础入门 Zookeeper，看了本文，你将会对 Zookeeper 有了初步的了解和认识。
注意：本文基于 Zookeeper 的版本是 3.4.14，最新版本的在使用上会有一些出入，但是企业现在使用的大部分都是 3.4x 版本的。

Zookeeper 概述
Zookeeper 是一个分布式协调服务">
<meta property="og:type" content="article">
<meta property="og:title" content="Zookeeper入门">
<meta property="og:url" content="https://chenjiabing666.github.io/2020/04/19/Zookeeper入门/index.html">
<meta property="og:site_name" content="不才陈某技术博客">
<meta property="og:description" content="导读
Zookeeper 相信大家都听说过，最典型的使用就是作为服务注册中心。今天陈某带大家从零基础入门 Zookeeper，看了本文，你将会对 Zookeeper 有了初步的了解和认识。
注意：本文基于 Zookeeper 的版本是 3.4.14，最新版本的在使用上会有一些出入，但是企业现在使用的大部分都是 3.4x 版本的。

Zookeeper 概述
Zookeeper 是一个分布式协调服务">
<meta property="og:updated_time" content="2020-04-19T07:48:31.119Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Zookeeper入门">
<meta name="twitter:description" content="导读
Zookeeper 相信大家都听说过，最典型的使用就是作为服务注册中心。今天陈某带大家从零基础入门 Zookeeper，看了本文，你将会对 Zookeeper 有了初步的了解和认识。
注意：本文基于 Zookeeper 的版本是 3.4.14，最新版本的在使用上会有一些出入，但是企业现在使用的大部分都是 3.4x 版本的。

Zookeeper 概述
Zookeeper 是一个分布式协调服务">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://chenjiabing666.github.io/2020/04/19/Zookeeper入门/"/>





  <title>Zookeeper入门 | 不才陈某技术博客</title>
  








</head>



<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">不才陈某技术博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">微信公众号：码猿技术专栏</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://chenjiabing666.github.io/2020/04/19/Zookeeper入门/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="不才陈某">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/header.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="不才陈某技术博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Zookeeper入门</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-04-19T15:48:16+08:00">
                2020-04-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/杂谈/" itemprop="url" rel="index">
                    <span itemprop="name">杂谈</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2020/04/19/Zookeeper入门/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2020/04/19/Zookeeper入门/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
		  
		  
			  <span class="post-meta-divider">|</span>
			  <span id="busuanzi_value_page_pv"></span>次阅读
			

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4.9k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  19
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="导读"><a href="#导读" class="headerlink" title="导读"></a>导读</h2><ul>
<li>Zookeeper 相信大家都听说过，最典型的使用就是作为服务注册中心。今天陈某带大家从零基础入门 Zookeeper，看了本文，你将会对 Zookeeper 有了初步的了解和认识。</li>
<li>注意：本文基于 Zookeeper 的版本是 3.4.14，最新版本的在使用上会有一些出入，但是企业现在使用的大部分都是 3.4x 版本的。</li>
</ul>
<h2 id="Zookeeper-概述"><a href="#Zookeeper-概述" class="headerlink" title="Zookeeper 概述"></a>Zookeeper 概述</h2><ul>
<li>Zookeeper 是一个分布式协调服务的开源框架。主要用来解决分布式集群中应用系统的一致性问题，例如怎样避免同时操作同一数据造成脏读的问题。</li>
<li>ZooKeeper 本质上是一个分布式的小文件存储系统。提供基于类似于文件系 统的目录树方式的数据存储，并且可以对树中的节点进行有效管理。从而用来维护和监控你存储的数据的状态变化。通过监控这些数据状态的变化，从而可以达 到基于数据的集群管理。诸如：<code>统一命名服务</code>、<code>分布式配置管理</code>、<code>分布式消息队列</code>、<code>分布式锁</code>、<code>分布式协调</code>等功能。</li>
</ul>
<h2 id="Zookeeper-特性"><a href="#Zookeeper-特性" class="headerlink" title="Zookeeper 特性"></a>Zookeeper 特性</h2><ol>
<li><p><code>全局数据一致</code>：每个 server 保存一份相同的数据副本，client 无论连 接到哪个 server，展示的数据都是一致的，这是最重要的特征；</p>
</li>
<li><p><code>可靠性</code>：如果消息被其中一台服务器接受，那么将被所有的服务器接受。</p>
</li>
<li><p><code>顺序性</code>：包括全局有序和偏序两种：全局有序是指如果在一台服务器上 消息 a 在消息 b 前发布，则在所有 Server 上消息 a 都将在消息 b 前被 发布；偏序是指如果一个消息 b 在消息 a 后被同一个发送者发布，a 必将排在 b 前面。</p>
</li>
<li><p><code>数据更新原子性</code>：一次数据更新要么成功（半数以上节点成功），要么失 败，不存在中间状态；</p>
</li>
<li><p><code>实时性</code>：Zookeeper 保证客户端将在一个时间间隔范围内获得服务器的更新信息，或者服务器失效的信息。</p>
</li>
</ol>
<h2 id="Zookeeper-节点类型"><a href="#Zookeeper-节点类型" class="headerlink" title="Zookeeper 节点类型"></a>Zookeeper 节点类型</h2><ul>
<li>Znode 有两种，分别为临时节点和永久节点。<ul>
<li><code>临时节点</code>：该节点的生命周期依赖于创建它们的会话。一旦会话结束，临时节点将被自动删除，当然可以也可以手动删除。临时节点不允许拥有子节点。</li>
<li><code>永久节点</code>：该节点的生命周期不依赖于会话，并且只有在客户端显示执行删除操作的时候，他们才能被删除。</li>
</ul>
</li>
<li>节点的类型在创建时即被确定，并且不能改变。</li>
<li>Znode 还有一个序列化的特性，如果创建的时候指定的话，该 Znode 的名字后面会自动追加一个不断增加的序列号。序列号对于此节点的父节点来说是唯一的，这样便会记录每个子节点创建的先后顺序。它的格式为<code>&quot;%10d&quot;</code>(10 位数字,没有数值的数位用 0 补充，例如“0000000001”)。</li>
<li>这样便会存在四种类型的 Znode 节点，分类如下：<ul>
<li><code>PERSISTENT</code>：永久节点</li>
<li><code>EPHEMERAL</code>：临时节点</li>
<li><code>PERSISTENT_SEQUENTIAL</code>：永久节点、序列化</li>
<li><code>EPHEMERAL_SEQUENTIAL</code>：临时节点、序列化</li>
</ul>
</li>
</ul>
<h2 id="ZooKeeper-Watcher"><a href="#ZooKeeper-Watcher" class="headerlink" title="ZooKeeper Watcher"></a>ZooKeeper Watcher</h2><ul>
<li>ZooKeeper 提供了分布式数据发布/订阅功能，一个典型的发布/订阅模型系统定义了一种一对多的订阅关系，能让多个订阅者同时监听某一个主题对象，当这个主题对象自身状态变化时，会通知所有订阅者，使他们能够做出相应的处理。</li>
<li>触发事件种类很多，如：节点创建，节点删除，节点改变，子节点改变等。</li>
<li>总的来说可以概括 Watcher 为以下三个过程：客户端向服务端注册 Watcher、服务端事件发生触发 Watcher、客户端回调 Watcher 得到触发事件情况。</li>
</ul>
<h2 id="Watcher-机制特点"><a href="#Watcher-机制特点" class="headerlink" title="Watcher 机制特点"></a>Watcher 机制特点</h2><ul>
<li><p><code>一次性触发</code> ：事件发生触发监听，一个 watcher event 就会被发送到设置监听的客户端，这种效果是一次性的，后续再次发生同样的事件，不会再次触发。</p>
</li>
<li><p><code>事件封装</code> ：ZooKeeper 使用 WatchedEvent 对象来封装服务端事件并传递。WatchedEvent 包含了每一个事件的三个基本属性： <code>通知状态</code>（keeperState），<code>事件类型</code>（EventType）和<code>节点路径</code>（path）。</p>
</li>
<li><p><code>event 异步发送</code> ：watcher 的通知事件从服务端发送到客户端是异步的。</p>
</li>
<li><p><code>先注册再触发</code> ：Zookeeper 中的 watch 机制，必须客户端先去服务端注册监听，这样事件发送才会触发监听，通知给客户端。</p>
</li>
</ul>
<h2 id="常用-Shell-命令"><a href="#常用-Shell-命令" class="headerlink" title="常用 Shell 命令"></a>常用 Shell 命令</h2><h3 id="新增节点"><a href="#新增节点" class="headerlink" title="新增节点"></a>新增节点</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">create [<span class="_">-s</span>] [<span class="_">-e</span>] path data</div></pre></td></tr></table></figure>
<ul>
<li><code>-s</code>：表示创建有序节点</li>
<li><code>-e</code>：表示创建临时节点</li>
<li>创建持久化节点：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">create /<span class="built_in">test</span> 1234</div><div class="line"></div><div class="line"><span class="comment">## 子节点</span></div><div class="line">create /<span class="built_in">test</span>/node1 node1</div></pre></td></tr></table></figure>
<ul>
<li>创建持久化有序节点：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">## 完整的节点名称是a0000000001</span></div><div class="line">create /a a</div><div class="line">Created /a0000000001</div><div class="line"></div><div class="line"><span class="comment">## 完整的节点名称是b0000000002</span></div><div class="line">create /b b</div><div class="line">Created /b0000000002</div></pre></td></tr></table></figure>
<ul>
<li>创建临时节点：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">create <span class="_">-e</span> /a a</div></pre></td></tr></table></figure>
<ul>
<li>创建临时有序节点：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">## 完整的节点名称是a0000000001</span></div><div class="line">create <span class="_">-e</span> <span class="_">-s</span> /a a</div><div class="line">Created /a0000000001</div></pre></td></tr></table></figure>
<h3 id="更新节点"><a href="#更新节点" class="headerlink" title="更新节点"></a>更新节点</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> [path] [data] [version]</div></pre></td></tr></table></figure>
<ul>
<li><code>path</code>：节点路径</li>
<li><code>data</code>：数据</li>
<li><code>version</code>：版本号</li>
<li>修改节点数据：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> /<span class="built_in">test</span> aaa</div><div class="line"></div><div class="line"><span class="comment">## 修改子节点</span></div><div class="line"><span class="built_in">set</span> /<span class="built_in">test</span>/node1 bbb</div></pre></td></tr></table></figure>
<ul>
<li>基于数据版本号修改，如果修改的节点的版本号(<code>dataVersion</code>)不正确，拒绝修改</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> /<span class="built_in">test</span> aaa 1</div></pre></td></tr></table></figure>
<h3 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">delete [path] [version]</div></pre></td></tr></table></figure>
<ul>
<li><code>path</code>：节点路径</li>
<li><code>version</code>：版本号，版本号不正确拒绝删除</li>
<li>删除节点</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">delete /<span class="built_in">test</span></div><div class="line"></div><div class="line"><span class="comment">## 版本号删除</span></div><div class="line">delete /<span class="built_in">test</span> 2</div></pre></td></tr></table></figure>
<ul>
<li>递归删除，删除某个节点及后代</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">rmr /<span class="built_in">test</span></div></pre></td></tr></table></figure>
<h3 id="查看节点数据和状态"><a href="#查看节点数据和状态" class="headerlink" title="查看节点数据和状态"></a>查看节点数据和状态</h3><ul>
<li>命令格式如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">get path</div></pre></td></tr></table></figure>
<ul>
<li>获取节点详情：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="comment">## 获取节点详情</span></div><div class="line">get /node1</div><div class="line"></div><div class="line"><span class="comment">## 节点内容</span></div><div class="line">aaa</div><div class="line">cZxid = 0x6</div><div class="line">ctime = Sun Apr 05 14:50:10 CST 2020</div><div class="line">mZxid = 0x6</div><div class="line">mtime = Sun Apr 05 14:50:10 CST 2020</div><div class="line">pZxid = 0x7</div><div class="line">cversion = 1</div><div class="line">dataVersion = 0</div><div class="line">aclVersion = 0</div><div class="line">ephemeralOwner = 0x0</div><div class="line">dataLength = 3</div><div class="line">numChildren = 1</div></pre></td></tr></table></figure>
<ul>
<li>节点各个属性对应的含义如下：<ul>
<li><code>cZxid</code>：数据节点创建时的事务 ID。</li>
<li><code>ctime</code>：数据节点创建时间。</li>
<li><code>mZxid</code>：数据节点最后一次更新时的事务 ID。</li>
<li><code>mtime</code>：数据节点最后一次更新的时间。</li>
<li><code>pZxid</code>：数据节点的子节点最后一次被修改时的事务 ID。</li>
<li><code>cversion</code>：子节点的更改次数。</li>
<li><code>dataVersion</code>：节点数据的更改次数。</li>
<li><code>aclVersion</code>  ：节点 ACL 的更改次数。</li>
<li><code>ephemeralOwner</code>：如果节点是临时节点，则表示创建该节点的会话的 SessionID。如果节点是持久化节点，值为 0。</li>
<li><code>dataLength</code>  ：节点数据内容的长度。</li>
<li><code>numChildren</code>：数据节点当前的子节点的个数。</li>
</ul>
</li>
</ul>
<h3 id="查看节点状态"><a href="#查看节点状态" class="headerlink" title="查看节点状态"></a>查看节点状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">stat</span> path</div></pre></td></tr></table></figure>
<ul>
<li><code>stat</code>命令和<code>get</code>命令相似，不过这个命令不会返回节点的数据，只返回节点的状态属性。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">stat</span> /node1</div><div class="line"></div><div class="line"><span class="comment">## 节点状态信息，没有节点数据</span></div><div class="line">cZxid = 0x6</div><div class="line">ctime = Sun Apr 05 14:50:10 CST 2020</div><div class="line">mZxid = 0x6</div><div class="line">mtime = Sun Apr 05 14:50:10 CST 2020</div><div class="line">pZxid = 0x7</div><div class="line">cversion = 1</div><div class="line">dataVersion = 0</div><div class="line">aclVersion = 0</div><div class="line">ephemeralOwner = 0x0</div><div class="line">dataLength = 3</div><div class="line">numChildren = 1</div></pre></td></tr></table></figure>
<h3 id="查看节点列表"><a href="#查看节点列表" class="headerlink" title="查看节点列表"></a>查看节点列表</h3><ul>
<li>查看节点列表有<code>ls path</code>和<code>ls2 path</code>两个命令。后者是前者的增强，不仅会返回节点列表还会返回当前节点的状态信息。</li>
<li><code>ls path</code>：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">ls /</div><div class="line"></div><div class="line"><span class="comment">## 仅仅返回节点列表</span></div><div class="line">[zookeeper, node1]</div></pre></td></tr></table></figure>
<ul>
<li><code>ls2 path</code>：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">ls2 /</div><div class="line"></div><div class="line"><span class="comment">## 返回节点列表和当前节点的状态信息</span></div><div class="line">[zookeeper, node1]</div><div class="line">cZxid = 0x0</div><div class="line">ctime = Thu Jan 01 08:00:00 CST 1970</div><div class="line">mZxid = 0x0</div><div class="line">mtime = Thu Jan 01 08:00:00 CST 1970</div><div class="line">pZxid = 0x6</div><div class="line">cversion = 2</div><div class="line">dataVersion = 0</div><div class="line">aclVersion = 0</div><div class="line">ephemeralOwner = 0x0</div><div class="line">dataLength = 0</div><div class="line">numChildren = 2</div></pre></td></tr></table></figure>
<h3 id="监听器-get-path-watch"><a href="#监听器-get-path-watch" class="headerlink" title="监听器 get path watch"></a>监听器 get path watch</h3><ul>
<li>使用<code>get path watch</code>注册的监听器在<code>节点内容</code>发生改变时，向客户端发送通知，注意 Zookeeper 的触发器是一次性的，触发一次后会立即生效。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">get /node1 watch</div><div class="line"></div><div class="line"><span class="comment">## 改变节点数据</span></div><div class="line"><span class="built_in">set</span> /node1 bbb</div><div class="line"></div><div class="line"><span class="comment">## 监听到节点内容改变了</span></div><div class="line">WATCHER::</div><div class="line">WatchedEvent state:SyncConnected <span class="built_in">type</span>:NodeDataChanged path:/node1</div></pre></td></tr></table></figure>
<h3 id="监听器-stat-path-watch"><a href="#监听器-stat-path-watch" class="headerlink" title="监听器 stat path watch"></a>监听器 stat path watch</h3><ul>
<li><code>stat path watch</code>注册的监听器能够在<code>节点状态</code>发生改变时向客户端发出通知。比如节点数据改变、节点被删除等。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">stat</span> /node2 watch</div><div class="line"></div><div class="line"><span class="comment">## 删除节点node2</span></div><div class="line">delete /node2</div><div class="line"></div><div class="line"><span class="comment">## 监听器监听到了节点删除</span></div><div class="line">WATCHER::</div><div class="line">WatchedEvent state:SyncConnected <span class="built_in">type</span>:NodeDeleted path:/node2</div></pre></td></tr></table></figure>
<h3 id="监听器-ls-ls2-path-watch"><a href="#监听器-ls-ls2-path-watch" class="headerlink" title="监听器 ls/ls2 path watch"></a>监听器 ls/ls2 path watch</h3><ul>
<li>使用<code>ls path watch</code>或者<code>ls2 path watch</code>注册的监听器，能够监听到该节点下的子节点的<code>增加</code>和<code>删除</code>操作。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">ls /node1 watch</div><div class="line"></div><div class="line"><span class="comment">## 创建子节点</span></div><div class="line">create /node1/b b</div><div class="line"></div><div class="line"><span class="comment">## 监听到了子节点的新增</span></div><div class="line">WATCHER::</div><div class="line">WatchedEvent state:SyncConnected <span class="built_in">type</span>:NodeChildrenChanged path:/node1</div></pre></td></tr></table></figure>
<h2 id="Zookeeper-的-ACL-权限控制"><a href="#Zookeeper-的-ACL-权限控制" class="headerlink" title="Zookeeper 的 ACL 权限控制"></a>Zookeeper 的 ACL 权限控制</h2><ul>
<li>zookeeper 类似文件控制系统，client 可以创建，删除，修改，查看节点，那么如何做到权限控制的呢？zookeeper 的<code>access control list</code> 访问控制列表可以做到这一点。</li>
<li>ACL 权限控制，使用<code>scheme:id:permission</code>来标识。<ul>
<li><code>权限模式(scheme)</code>：授权的策略</li>
<li><code>授权对象(id)</code>：授权的对象</li>
<li><code>权限(permission)</code>：授予的权限</li>
</ul>
</li>
<li>权限控制是基于每个节点的，需要对每个节点设置权限。</li>
<li>每个节点支持设置多种权限控制方案和多个权限。</li>
<li>子节点不会继承父节点的权限，客户端无权访问某节点，但可能可以访问它的子节点。</li>
<li>例如：根据 IP 地址进行授权，命令如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span>ACl /node1 ip:192.168.10.1:crdwa</div></pre></td></tr></table></figure>
<h3 id="权限模式"><a href="#权限模式" class="headerlink" title="权限模式"></a>权限模式</h3><ul>
<li>权限模式即是采用何种方式授权。</li>
<li><code>world</code>：只有一个用户，anyone，表示登录 zookeeper 所有人（默认的模式）。</li>
<li><code>ip</code>：对客户端使用 IP 地址认证。</li>
<li><code>auth</code>：使用已添加认证的用户认证。</li>
<li><code>digest</code>：使用<code>用户名:密码</code>方式认证。</li>
</ul>
<h3 id="授权对象"><a href="#授权对象" class="headerlink" title="授权对象"></a>授权对象</h3><ul>
<li>给谁授权，授权对象的 ID 指的是权限赋予的实体，例如 IP 地址或用户。</li>
</ul>
<h3 id="授予的权限"><a href="#授予的权限" class="headerlink" title="授予的权限"></a>授予的权限</h3><ul>
<li>授予的权限包括<code>create</code>、<code>delete</code>、<code>read</code>、<code>writer</code>、<code>admin</code>。也就是增、删、改、查、管理的权限，简写<code>cdrwa</code>。</li>
<li><strong>注意</strong>：以上 5 种权限中，<code>delete</code>是指对子节点的删除权限，其他 4 种权限是对自身节点的操作权限。</li>
<li><code>create</code>：简写<code>c</code>，可以创建子节点。</li>
<li><code>delete</code>：简写<code>d</code>，可以删除子节点（仅下一级节点）。</li>
<li><code>read</code>：简写<code>r</code>，可以读取节点数据以及显示子节点列表。</li>
<li><code>write</code>：简写<code>w</code>，可以更改节点数据。</li>
<li><code>admin</code>：简写<code>a</code>，可以设置节点访问控制列表权限。</li>
</ul>
<h3 id="授权相关命令"><a href="#授权相关命令" class="headerlink" title="授权相关命令"></a>授权相关命令</h3><ul>
<li><code>getAcl [path]</code>：读取指定节点的 ACL 权限。</li>
<li><code>setAcl [path] [acl]</code>：设置 ACL</li>
<li><code>addauth &lt;scheme&gt; &lt;auth&gt;</code>：添加认证用户，和 auth，digest 授权模式相关。</li>
</ul>
<h3 id="world-授权模式案例"><a href="#world-授权模式案例" class="headerlink" title="world 授权模式案例"></a>world 授权模式案例</h3><ul>
<li>zookeeper 中默认的授权模式，针对登录 zookeeper 的任何用户授予指定的权限。命令如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span>Acl [path] world:anyone:[permission]</div></pre></td></tr></table></figure>
<ul>
<li><code>path</code>：节点</li>
<li><code>permission</code>：授予的权限，比如<code>cdrwa</code></li>
<li>去掉不能读取节点数据的权限：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="comment">## 获取权限列表（默认的）</span></div><div class="line">getAcl /node2</div><div class="line"></div><div class="line"><span class="string">'world,'</span>anyone</div><div class="line">: cdrwa</div><div class="line"></div><div class="line"><span class="comment">## 去掉读取节点数据的的权限，去掉r</span></div><div class="line"><span class="built_in">set</span>Acl /node2 world:anyone:cdwa</div><div class="line"></div><div class="line"><span class="comment">## 再次获取权限列表</span></div><div class="line">getAcl /node2</div><div class="line"></div><div class="line"><span class="string">'world,'</span>anyone</div><div class="line">: cdwa</div><div class="line"></div><div class="line"><span class="comment">## 获取节点数据，没有权限，失败</span></div><div class="line">get /node2</div><div class="line"></div><div class="line">Authentication is not valid : /node2</div></pre></td></tr></table></figure>
<h3 id="IP-授权模式案例"><a href="#IP-授权模式案例" class="headerlink" title="IP 授权模式案例"></a>IP 授权模式案例</h3><ul>
<li>针对登录用户的 ip 进行限制权限。命令如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span>Acl [path] ip:[ip]:[acl]</div></pre></td></tr></table></figure>
<ul>
<li>远程登录 zookeeper 的命令如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./zkCli.sh -server ip</div></pre></td></tr></table></figure>
<ul>
<li>设置<code>192.168.10.1</code>这个 ip 的增删改查管理的权限。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span>Acl /node2 ip:192.168.10.1:crdwa</div></pre></td></tr></table></figure>
<h3 id="Auth-授权模式案例"><a href="#Auth-授权模式案例" class="headerlink" title="Auth 授权模式案例"></a>Auth 授权模式案例</h3><ul>
<li>auth 授权模式需要有一个认证用户，添加命令如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">addauth digest [username]:[password]</div></pre></td></tr></table></figure>
<ul>
<li>设置 auth 授权模式命令如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span>Acl [path] auth:[user]:[acl]</div></pre></td></tr></table></figure>
<ul>
<li>为<code>chenmou</code>这个账户添加 cdrwa 权限：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">## 添加一个认证账户</span></div><div class="line">addauth digest chenmou:123456</div><div class="line"></div><div class="line"><span class="comment">## 添加权限</span></div><div class="line"><span class="built_in">set</span>Acl /node2 auth:chenmou:crdwa</div></pre></td></tr></table></figure>
<h3 id="多种模式授权"><a href="#多种模式授权" class="headerlink" title="多种模式授权"></a>多种模式授权</h3><ul>
<li>zookeeper 中同一个节点可以使用多种授权模式，多种授权模式用<code>,</code>分隔。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">## 创建节点</span></div><div class="line">create /node3</div><div class="line"></div><div class="line"><span class="comment">## 添加认证用户</span></div><div class="line">addauth chenmou:123456</div><div class="line"></div><div class="line"><span class="comment">## 添加多种授权模式</span></div><div class="line"><span class="built_in">set</span>Acl /node3 ip:192.178.10.1:crdwa,auth:chenmou:crdwa</div></pre></td></tr></table></figure>
<h3 id="ACL-超级管理员"><a href="#ACL-超级管理员" class="headerlink" title="ACL 超级管理员"></a>ACL 超级管理员</h3><ul>
<li>zookeeper 的权限管理模式有一种叫做<code>super</code>，该模式提供一个超管可以方便的访问任何权限的节点。</li>
<li>假设这个超管是<code>super:admin</code>，需要先为超管生成密码的密文：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">echo</span> -n super:admin | openssl dgst  -binary -sha1 |openssl base64</div><div class="line"></div><div class="line"><span class="comment">## 执行完生成了秘钥</span></div><div class="line">xQJmxLMiHGwaqBvst5y6rkB6HQs=</div></pre></td></tr></table></figure>
<ul>
<li>打开<code>zookeeper</code>目录下<code>/bin/zkServer.sh</code>，找到如下一行：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">nohup JAVA&amp;quot;−Dzookeeper.log.dir=JAVA<span class="string">"−Dzookeeper.log.dir=&#123;ZOO_LOG_DIR&#125;"</span> <span class="string">"-Dzookeeper.root.logger=<span class="variable">$&#123;ZOO_LOG4J_PROP&#125;</span>"</span></div></pre></td></tr></table></figure>
<ul>
<li>在后面添加一行脚本，如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="string">"-Dzookeeper.DigestAuthenticationProvider.superDigest=super:xQJmxLMiHGwaqBvst5y6rkB6HQs="</span></div></pre></td></tr></table></figure>
<ul>
<li>此时完整的脚本如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">nohup <span class="string">"<span class="variable">$JAVA</span>"</span> <span class="string">"-Dzookeeper.log.dir=<span class="variable">$&#123;ZOO_LOG_DIR&#125;</span>"</span> <span class="string">"-Dzookeeper.root.logger=<span class="variable">$&#123;ZOO_LOG4J_PROP&#125;</span>"</span> <span class="string">"-Dzookeeper.DigestAuthenticationProvider.superDigest=super:xQJmxLMiHGwaqBvst5y6rkB6HQs="</span> \</div><div class="line">    -cp <span class="string">"<span class="variable">$CLASSPATH</span>"</span> <span class="variable">$JVMFLAGS</span> <span class="variable">$ZOOMAIN</span> <span class="string">"<span class="variable">$ZOOCFG</span>"</span> &gt; <span class="string">"<span class="variable">$_ZOO_DAEMON_OUT</span>"</span> 2&gt;&amp;1 &lt; /dev/null &amp;</div></pre></td></tr></table></figure>
<ul>
<li>重启 zookeeper</li>
<li>重启完成之后此时超管即配置完成，如果需要使用，则使用如下命令：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">addauth digest super:admin</div></pre></td></tr></table></figure>
<h2 id="Curator-客户端"><a href="#Curator-客户端" class="headerlink" title="Curator 客户端"></a>Curator 客户端</h2><ul>
<li>Curator 是 Netflix 公司开源的一个 Zookeeper 客户端，与 Zookeeper 提供的原生客户端相比，Curator 的抽象层次更高，简化了 Zookeeper 客户端的开发量。</li>
</ul>
<h3 id="添加依赖"><a href="#添加依赖" class="headerlink" title="添加依赖"></a>添加依赖</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-recipes<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></div><div class="line">               <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></div><div class="line">                   <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">                   <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">               <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></div><div class="line">           <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></div><div class="line">       <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div><div class="line"></div><div class="line">       <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></div><div class="line">           <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></div><div class="line">       <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></div></pre></td></tr></table></figure>
<h3 id="建立连接"><a href="#建立连接" class="headerlink" title="建立连接"></a>建立连接</h3><ul>
<li>客户端建立与 Zookeeper 的连接，这里仅仅演示单机版本的连接，如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//创建CuratorFramework，用来操作api</span></div><div class="line">CuratorFramework  client = CuratorFrameworkFactory.builder()</div><div class="line">    <span class="comment">//ip地址+端口号，如果是集群，逗号分隔</span></div><div class="line">    .connectString(<span class="string">"120.26.101.207:2181"</span>)</div><div class="line">    <span class="comment">//会话超时时间</span></div><div class="line">    .sessionTimeoutMs(<span class="number">5000</span>)</div><div class="line">    <span class="comment">//超时重试策略,RetryOneTime：超时重连仅仅一次</span></div><div class="line">    .retryPolicy(<span class="keyword">new</span> RetryOneTime(<span class="number">3000</span>))</div><div class="line">    <span class="comment">//命名空间，父节点，如果不指定是在根节点下</span></div><div class="line">    .namespace(<span class="string">"node4"</span>)</div><div class="line">    .build();</div><div class="line"><span class="comment">//启动</span></div><div class="line">client.start();</div></pre></td></tr></table></figure>
<h3 id="重连策略"><a href="#重连策略" class="headerlink" title="重连策略"></a>重连策略</h3><ul>
<li>会话连接策略，即是当客户端与 Zookeeper 断开连接之后，客户端重新连接 Zookeeper 时使用的策略，比如重新连接一次。</li>
<li><code>RetryOneTime：</code>N 秒后重连一次，仅仅一次，演示如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">.retryPolicy(<span class="keyword">new</span> RetryOneTime(<span class="number">3000</span>))</div></pre></td></tr></table></figure>
<ul>
<li><code>RetryNTimes</code>：每 n 秒重连一次，重连 m 次。演示如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//每三秒重连一次，重连3次。arg1：多长时间后重连，单位毫秒，arg2：总共重连几次</span></div><div class="line">.retryPolicy(<span class="keyword">new</span> RetryNTimes(<span class="number">3000</span>,<span class="number">3</span>))</div></pre></td></tr></table></figure>
<ul>
<li><code>RetryUntilElapsed</code>：设置了最大等待时间，如果超过这个最大等待时间将会不再连接。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//每三秒重连一次，等待时间超过10秒不再重连。arg1：总等待时间，arg2：多长时间重连，单位毫秒</span></div><div class="line">.retryPolicy(<span class="keyword">new</span> RetryUntilElapsed(<span class="number">10000</span>,<span class="number">3000</span>))</div></pre></td></tr></table></figure>
<h3 id="新增节点-1"><a href="#新增节点-1" class="headerlink" title="新增节点"></a>新增节点</h3><ul>
<li>新增节点</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">client.create()</div><div class="line">    <span class="comment">//指定节点的类型。PERSISTENT：持久化节点，PERSISTENT_SEQUENTIAL：持久化有序节点，EPHEMERAL：临时节点，EPHEMERAL_SEQUENTIAL临时有序节点</span></div><div class="line">    .withMode(CreateMode.PERSISTENT)</div><div class="line">    <span class="comment">//指定权限列表，OPEN_ACL_UNSAFE：world:anyone:crdwa</span></div><div class="line">    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)</div><div class="line">    <span class="comment">//写入节点数据，arg1:节点名称 arg2:节点数据</span></div><div class="line">    .forPath(<span class="string">"/a"</span>, <span class="string">"a"</span>.getBytes());</div></pre></td></tr></table></figure>
<ul>
<li>自定义权限列表：<code>withACL(acls)</code>方法中可以设置自定义的权限列表，代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//自定义权限列表</span></div><div class="line">List&lt;ACL&gt; acls=<span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line"><span class="comment">//指定授权模式和授权对象 arg1:授权模式，arg2授权对象</span></div><div class="line">Id id=<span class="keyword">new</span> Id(<span class="string">"ip"</span>,<span class="string">"127.0.0.1"</span>);</div><div class="line"><span class="comment">//指定授予的权限，ZooDefs.Perms.ALL:crdwa</span></div><div class="line">acls.add(<span class="keyword">new</span> ACL(ZooDefs.Perms.ALL,id));</div><div class="line">client.create()</div><div class="line">    .withMode(CreateMode.PERSISTENT)</div><div class="line">    <span class="comment">//指定自定义权限列表</span></div><div class="line">    .withACL(acls)</div><div class="line">    .forPath(<span class="string">"/b"</span>, <span class="string">"b"</span>.getBytes());</div></pre></td></tr></table></figure>
<ul>
<li>递归创建节点：<code>creatingParentsIfNeeded()</code>方法对于创建多层节点，如果其中一个节点不存在的话会自动创建</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//递归创建节点</span></div><div class="line">client.create()</div><div class="line">    <span class="comment">//递归方法，如果节点不存在，那么创建该节点</span></div><div class="line">    .creatingParentsIfNeeded()</div><div class="line">    .withMode(CreateMode.PERSISTENT)</div><div class="line">    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)</div><div class="line">    <span class="comment">//test节点和b节点不存在，递归创建出来</span></div><div class="line">    .forPath(<span class="string">"/test/a"</span>, <span class="string">"a"</span>.getBytes());</div></pre></td></tr></table></figure>
<ul>
<li>异步创建节点：<code>inBackground()</code>方法可以异步回调创建节点，创建完成后会自动回调实现的方法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"> <span class="comment">//异步创建节点</span></div><div class="line">client.create()</div><div class="line">    .withMode(CreateMode.PERSISTENT)</div><div class="line">    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)</div><div class="line">    <span class="comment">//异步创建</span></div><div class="line">    .inBackground(<span class="keyword">new</span> BackgroundCallback() &#123;</div><div class="line">        <span class="comment">/**</span></div><div class="line">        * <span class="doctag">@param</span> curatorFramework 客户端对象</div><div class="line">        * <span class="doctag">@param</span> curatorEvent 事件对象</div><div class="line">        */</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">processResult</span><span class="params">(CuratorFramework curatorFramework, CuratorEvent curatorEvent)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">            <span class="comment">//打印事件类型</span></div><div class="line">            System.out.println(curatorEvent.getType());</div><div class="line">            &#125;</div><div class="line">    &#125;)</div><div class="line">    .forPath(<span class="string">"/test1"</span>, <span class="string">"a"</span>.getBytes());</div></pre></td></tr></table></figure>
<h3 id="更新节点数据"><a href="#更新节点数据" class="headerlink" title="更新节点数据"></a>更新节点数据</h3><ul>
<li>更新节点，当节点不存在会报错，代码如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">client.setData()</div><div class="line">      .forPath(<span class="string">"/a"</span>,<span class="string">"a"</span>.getBytes());</div></pre></td></tr></table></figure>
<ul>
<li>携带版本号更新节点，当版本错误拒绝更新</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">client.setData()</div><div class="line">    <span class="comment">//指定版本号更新，如果版本号错误则拒绝更新</span></div><div class="line">    .withVersion(<span class="number">1</span>)</div><div class="line">    .forPath(<span class="string">"/a"</span>,<span class="string">"a"</span>.getBytes());</div></pre></td></tr></table></figure>
<ul>
<li>异步更新节点数据：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">client.setData()</div><div class="line">        <span class="comment">//异步更新</span></div><div class="line">        .inBackground(<span class="keyword">new</span> BackgroundCallback() &#123;</div><div class="line">        <span class="comment">//回调方法</span></div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">processResult</span><span class="params">(CuratorFramework curatorFramework, CuratorEvent curatorEvent)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">                   &#125;</div><div class="line">        &#125;)</div><div class="line">		.forPath(<span class="string">"/a"</span>,<span class="string">"a"</span>.getBytes());</div></pre></td></tr></table></figure>
<h3 id="删除节点-1"><a href="#删除节点-1" class="headerlink" title="删除节点"></a>删除节点</h3><ul>
<li>删除当前节点，如果有子节点则拒绝删除</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">client.delete()</div><div class="line">    <span class="comment">//删除节点，如果是该节点包含子节点，那么不能删除</span></div><div class="line">    .forPath(<span class="string">"/a"</span>);</div></pre></td></tr></table></figure>
<ul>
<li>指定版本号删除，如果版本错误则拒绝删除</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">client.delete()</div><div class="line">    <span class="comment">//指定版本号删除</span></div><div class="line">    .withVersion(<span class="number">1</span>)</div><div class="line">    <span class="comment">//删除节点，如果是该节点包含子节点，那么不能删除</span></div><div class="line">    .forPath(<span class="string">"/a"</span>);</div></pre></td></tr></table></figure>
<ul>
<li>如果当前节点包含子节点则一并删除，使用<code>deletingChildrenIfNeeded()</code>方法</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">client.delete()</div><div class="line">    <span class="comment">//如果删除的节点包含子节点则一起删除</span></div><div class="line">    .deletingChildrenIfNeeded()</div><div class="line">    <span class="comment">//删除节点，如果是该节点包含子节点，那么不能删除</span></div><div class="line">    .forPath(<span class="string">"/a"</span>);</div></pre></td></tr></table></figure>
<ul>
<li>异步删除节点，使用<code>inBackground()</code></li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">client.delete()</div><div class="line">	.deletingChildrenIfNeeded()</div><div class="line">	<span class="comment">//异步删除节点</span></div><div class="line">    .inBackground(<span class="keyword">new</span> BackgroundCallback() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">processResult</span><span class="params">(CuratorFramework curatorFramework, CuratorEvent curatorEvent)</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">        <span class="comment">//回调监听</span></div><div class="line">        &#125;</div><div class="line">       &#125;)</div><div class="line">    <span class="comment">//删除节点，如果是该节点包含子节点，那么不能删除</span></div><div class="line">    .forPath(<span class="string">"/a"</span>);</div></pre></td></tr></table></figure>
<h3 id="获取节点数据"><a href="#获取节点数据" class="headerlink" title="获取节点数据"></a>获取节点数据</h3><ul>
<li>同步获取节点数据</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">byte</span>[] bytes = client.getData().forPath(<span class="string">"/node1"</span>);</div><div class="line">System.out.println(<span class="keyword">new</span> String(bytes));</div></pre></td></tr></table></figure>
<ul>
<li>获取节点状态和数据</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//保存节点状态</span></div><div class="line">Stat stat=<span class="keyword">new</span> Stat();</div><div class="line"><span class="keyword">byte</span>[] bytes = client.getData()</div><div class="line">	<span class="comment">//获取节点状态存储在stat对象中</span></div><div class="line">    .storingStatIn(stat)</div><div class="line">    .forPath(<span class="string">"/node1"</span>);</div><div class="line">System.out.println(<span class="keyword">new</span> String(bytes));</div><div class="line"><span class="comment">//获取节点数据的长度</span></div><div class="line">System.out.println(stat.getDataLength());</div></pre></td></tr></table></figure>
<ul>
<li>异步获取节点数据</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">client.getData()</div><div class="line">    <span class="comment">//异步获取节点数据，回调监听</span></div><div class="line">     .inBackground((curatorFramework, curatorEvent) -&gt; &#123;</div><div class="line">          <span class="comment">//节点数据</span></div><div class="line">          System.out.println(<span class="keyword">new</span> String(curatorEvent.getData()));</div><div class="line">      &#125;)</div><div class="line">     .forPath(<span class="string">"/node1"</span>);</div></pre></td></tr></table></figure>
<h3 id="获取子节点"><a href="#获取子节点" class="headerlink" title="获取子节点"></a>获取子节点</h3><ul>
<li>同步获取全部子节点</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">List&lt;String&gt; strs = client.getChildren().forPath(<span class="string">"/"</span>);</div><div class="line">       <span class="keyword">for</span> (String str:strs) &#123;</div><div class="line">           System.out.println(str);</div><div class="line">       &#125;</div></pre></td></tr></table></figure>
<ul>
<li>异步获取全部子节点</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">client.getChildren()</div><div class="line"><span class="comment">//异步获取</span></div><div class="line">.inBackground((curatorFramework, curatorEvent) -&gt; &#123;</div><div class="line">        List&lt;String&gt; strs = curatorEvent.getChildren();</div><div class="line">        <span class="keyword">for</span> (String str:strs) &#123;</div><div class="line">              System.out.println(str);</div><div class="line">        &#125;</div><div class="line">  &#125;)</div><div class="line">.forPath(<span class="string">"/"</span>);</div></pre></td></tr></table></figure>
<h3 id="查看节点是否存在"><a href="#查看节点是否存在" class="headerlink" title="查看节点是否存在"></a>查看节点是否存在</h3><ul>
<li>同步查看</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//如果节点不存在，stat为null</span></div><div class="line">Stat stat = client.checkExists().forPath(<span class="string">"/node"</span>);</div></pre></td></tr></table></figure>
<ul>
<li>异步查看</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//如果节点不存在，stat为null</span></div><div class="line">client.checkExists()</div><div class="line">    .inBackground((curatorFramework, curatorEvent) -&gt; &#123;</div><div class="line">    <span class="comment">//如果为null则不存在</span></div><div class="line">    System.out.println(curatorEvent.getStat());</div><div class="line">    &#125;)</div><div class="line">    .forPath(<span class="string">"/node"</span>);</div></pre></td></tr></table></figure>
<h3 id="Watcher-API"><a href="#Watcher-API" class="headerlink" title="Watcher API"></a>Watcher API</h3><ul>
<li>curator 提供了两种 watcher 来监听节点的变化<ul>
<li><code>NodeCache</code>：监听一个特定的节点，监听新增和修改</li>
<li><code>PathChildrenCache</code>：监听一个节点的子节点，当一个子节点增加、删除、更新时，path Cache 会改变他的状态，会包含最新的子节点的数据和状态。</li>
</ul>
</li>
<li>NodeCache 演示：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//arg1:连接对象 arg2:监听的节点路径,/namespace/path</span></div><div class="line"><span class="keyword">final</span> NodeCache nodeCache = <span class="keyword">new</span> NodeCache(client, <span class="string">"/w1"</span>);</div><div class="line"><span class="comment">//启动监听</span></div><div class="line">nodeCache.start();</div><div class="line"><span class="comment">//添加监听器</span></div><div class="line">nodeCache.getListenable().addListener(() -&gt; &#123;</div><div class="line">    <span class="comment">//节点路径</span></div><div class="line">    System.out.println(nodeCache.getCurrentData().getPath());</div><div class="line">    <span class="comment">//节点数据</span></div><div class="line">    System.out.println(<span class="keyword">new</span> String(nodeCache.getCurrentData().getData()));</div><div class="line">&#125;);</div><div class="line"><span class="comment">//睡眠100秒</span></div><div class="line">Thread.sleep(<span class="number">1000000</span>);</div><div class="line"><span class="comment">//关闭监听</span></div><div class="line">nodeCache.close();</div></pre></td></tr></table></figure>
<ul>
<li><code>PathChildrenCache</code>演示：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">//arg1：连接对象 arg2：节点路径  arg3:是否能够获取节点数据</span></div><div class="line">PathChildrenCache cache=<span class="keyword">new</span> PathChildrenCache(client,<span class="string">"/w1"</span>, <span class="keyword">true</span>);</div><div class="line">cache.start();</div><div class="line">cache.getListenable().addListener((curatorFramework, pathChildrenCacheEvent) -&gt; &#123;</div><div class="line">	<span class="comment">//节点路径</span></div><div class="line">	System.out.println(pathChildrenCacheEvent.getData().getPath());</div><div class="line">	<span class="comment">//节点状态</span></div><div class="line">	System.out.println(pathChildrenCacheEvent.getData().getStat());</div><div class="line">	<span class="comment">//节点数据</span></div><div class="line">	System.out.println(<span class="keyword">new</span> String(pathChildrenCacheEvent.getData().getData()));</div><div class="line">&#125;);</div><div class="line">cache.close();</div></pre></td></tr></table></figure>
<h2 id="小福利"><a href="#小福利" class="headerlink" title="小福利"></a>小福利</h2><ul>
<li>是不是觉得文章太长看得头晕脑胀，为此陈某特地将本篇文章制作成 PDF 文本，需要回去仔细研究的朋友，老规矩，回复关键词<code>ZK入门指南</code>。</li>
</ul>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>写的可还行？</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat.jpg" alt="不才陈某 微信支付"/>
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    
	<div>
	
<div>
      
        
      
</div>

  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
  
</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/杂谈/" rel="tag"><i class="fa fa-tag"></i> 杂谈</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/19/Mysql中orderby底层执行流程/" rel="next" title="Mysql中orderby底层执行流程">
                <i class="fa fa-chevron-left"></i> Mysql中orderby底层执行流程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/04/19/Zookeeper实现分布式锁/" rel="prev" title="Zookeeper实现分布式锁">
                Zookeeper实现分布式锁 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/header.jpg"
                alt="不才陈某" />
            
              <p class="site-author-name" itemprop="name">不才陈某</p>
              <p class="site-description motion-element" itemprop="description">本站是不才陈某的技术分享博客。内容涵盖Java后端技术、Spring Boot、微服务架构、系统安全、前端、系统监控等相关的研究与知识分享。</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">63</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://juejin.im/user/1556564194374926" target="_blank" title="掘金">
                      
                        <i class="fa fa-fw fa-globe"></i>掘金</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.zhihu.com/people/xiao-ming-64-34-45" target="_blank" title="知乎">
                      
                        <i class="fa fa-fw fa-globe"></i>知乎</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/qq_34162294" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-globe"></i>CSDN</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/chenjiabing666/BlogImage/raw/master/myjszl_log.png" target="_blank" title="微信公众号">
                      
                        <i class="fa fa-fw fa-globe"></i>微信公众号</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                推荐阅读
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.pexels.com/zh-cn/" title="Pexels" target="_blank">Pexels</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.ityouknow.com/" title="纯洁的微笑" target="_blank">纯洁的微笑</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#导读"><span class="nav-text">导读</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Zookeeper-概述"><span class="nav-text">Zookeeper 概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Zookeeper-特性"><span class="nav-text">Zookeeper 特性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Zookeeper-节点类型"><span class="nav-text">Zookeeper 节点类型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ZooKeeper-Watcher"><span class="nav-text">ZooKeeper Watcher</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Watcher-机制特点"><span class="nav-text">Watcher 机制特点</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#常用-Shell-命令"><span class="nav-text">常用 Shell 命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#新增节点"><span class="nav-text">新增节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更新节点"><span class="nav-text">更新节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除节点"><span class="nav-text">删除节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看节点数据和状态"><span class="nav-text">查看节点数据和状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看节点状态"><span class="nav-text">查看节点状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看节点列表"><span class="nav-text">查看节点列表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#监听器-get-path-watch"><span class="nav-text">监听器 get path watch</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#监听器-stat-path-watch"><span class="nav-text">监听器 stat path watch</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#监听器-ls-ls2-path-watch"><span class="nav-text">监听器 ls/ls2 path watch</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Zookeeper-的-ACL-权限控制"><span class="nav-text">Zookeeper 的 ACL 权限控制</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#权限模式"><span class="nav-text">权限模式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#授权对象"><span class="nav-text">授权对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#授予的权限"><span class="nav-text">授予的权限</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#授权相关命令"><span class="nav-text">授权相关命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#world-授权模式案例"><span class="nav-text">world 授权模式案例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#IP-授权模式案例"><span class="nav-text">IP 授权模式案例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Auth-授权模式案例"><span class="nav-text">Auth 授权模式案例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多种模式授权"><span class="nav-text">多种模式授权</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ACL-超级管理员"><span class="nav-text">ACL 超级管理员</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Curator-客户端"><span class="nav-text">Curator 客户端</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#添加依赖"><span class="nav-text">添加依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#建立连接"><span class="nav-text">建立连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#重连策略"><span class="nav-text">重连策略</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#新增节点-1"><span class="nav-text">新增节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#更新节点数据"><span class="nav-text">更新节点数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除节点-1"><span class="nav-text">删除节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取节点数据"><span class="nav-text">获取节点数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取子节点"><span class="nav-text">获取子节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查看节点是否存在"><span class="nav-text">查看节点是否存在</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Watcher-API"><span class="nav-text">Watcher API</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#小福利"><span class="nav-text">小福利</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">不才陈某</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">143.1k</span>
  
</div>

<!--





  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>

-->

<div>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<span id="busuanzi_container_site_pv" style='display:none'>
    本站总访问量 <span id="busuanzi_value_site_pv"></span> 次
    <span class="post-meta-divider">|</span>
</span>
<span id="busuanzi_container_site_uv" style='display:none'>
    有<span id="busuanzi_value_site_uv"></span>人看过我的博客啦
</span>
</div>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共143.1k字</span>
</div>

        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'RtlQOgOT3M0hgyMeogykSQjT-gzGzoHsz',
        appKey: 'vSCxLU88wnXVf0Pa8mwJrCPm',
        placeholder: '原创不易，欢迎各位指正',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  

  

  

</body>
</html>
